<?php

class AdminController extends Controller
{

    private $_model;

    public $menu = array(
        array(
            'label' =>  '平台管理',
        ),
        array(
            'label' =>  '所有用户',
            'url'   =>  array('/admin/index'),
        ),
        array(
            'label' =>  '新增用户',
            'url'   =>  array('/admin/createUser'),
        ),
        array(
            'label' =>  '所有产品线',
            'url'   =>  array('/admin/listProduct'),
        ),
        array(
            'label' =>  '新增产品线',
            'url'   =>  array('/admin/createProduct'),
        ),
        array(
            'label' =>  '所有存储系统',
            'url'   =>  array('/admin/listSystem'),
        ),
        array(
            'label' =>  '新增存储系统',
            'url'   =>  array('/admin/createSystem'),
        ),
        array(
            'label' =>  '工作流',
            'url'   =>  array('/workflow/index'),
        ),
    );
    
    /**
     * @return array action filters
     * 
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }
    
    public function accessRules()
    {
        return array(
            array('allow', // allow authenticated admin users to access all actions
                'users'=>array('@'),
                'expression'=>'$user->role == Role::Admin',
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }
        
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/job/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		
        $criteria = new CDbCriteria(array(
            'select' => 'user_id, user_name, role',
            )
        );
        
	    $dataProvider = new CActiveDataProvider('User', array(
            	'pagination'    =>  array(
                'pageSize'  =>  20,
            ),
            'criteria'  =>  $criteria,
        ));
        $this->render('index', array(
            'dataProvider'  => $dataProvider,
        ));
	}
	
 	/**
     *  Creates a new model.
     *  If creation is successful, the browser will be redirected to the 'view' page.
     * 
     */
    public function actionCreateUser()
    {
        $model=new User;
        if(isset($_POST['User']))
        {
            $model->user_name=$_POST['User']['user_name'];
            $model->password=md5($_POST['User']['password']);
            $model->role=$_POST['User']['role'];
            if($model->save()) {
                $this->redirect(array('index'));
            }          
        }

        $this->render('createuser',array(
            'model'=>$model,
        ));
    }

    /**
     *   Deletes a particular model.
     *   If deletion is successful, the browser will be redirected to the 'index' page.
     */
    public function actionDeleteUser()
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $this->loadModel()->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(array('index'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}
    
    /**
     *  Creates a new model.
     *  If creation is successful, the browser will be redirected to the 'view' page.
     * 
     */
    public function actionCreateProduct()
    {
        $model=new Product;
        if(isset($_POST['Product']))
        {
            $model->product_name=$_POST['Product']['product_name'];
            if($model->save()) {
                $this->redirect(array('listProduct'));
            }          
        }

        $this->render('createproduct',array(
            'model'=>$model,
        ));
    }
	
    public function actionListProduct()
    {
        $criteria = new CDbCriteria;
        
	    $dataProvider = new CActiveDataProvider('Product', array(
            	'pagination'    =>  array(
                'pageSize'  =>  20,
            ),
            'criteria'  =>  $criteria,
        ));
        $this->render('productlist', array(
            'dataProvider'  => $dataProvider,
        ));
    }
    
    /**
     *   Deletes a particular model.
     *   If deletion is successful, the browser will be redirected to the 'index' page.
     */
    public function actionDeleteProduct()
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            if(isset($_GET['id']))
            {
                $model=Product::model()->findByPk($_GET['id']);
                $model->delete();
            }

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(array('listProduct'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }
    
	public function actionListSystem()
    {
        $criteria = new CDbCriteria;
        
	    $dataProvider = new CActiveDataProvider('StorageSystem', array(
            	'pagination'    =>  array(
                'pageSize'  =>  20,
            ),
            'criteria'  =>  $criteria,
        ));
        $this->render('systemlist', array(
            'dataProvider'  => $dataProvider,
        ));
    }
    
    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     */
    public function actionUpdateSystem($id)
    {
        $model=StorageSystem::model()->findByPk($id);
        if(isset($_POST['StorageSystem']))
        {
            $model->attributes=$_POST['StorageSystem'];
            $model->port = $_POST['StorageSystem']['port'];
            $model->user_name = $_POST['StorageSystem']['user_name'];
            $model->password = $_POST['StorageSystem']['password'];
            if($model->save())
                $this->redirect(array('listSystem'));
        }

        $this->render('createsystem',array(
            'model'=>$model,
        ));
    }

	/**
     *  Creates a new model.
     *  If creation is successful, the browser will be redirected to the 'view' page.
     * 
     */
    public function actionCreateSystem()
    {
        $model=new StorageSystem;
        if(isset($_POST['StorageSystem']))
        {
            $model->attributes=$_POST['StorageSystem'];
            $model->port = $_POST['StorageSystem']['port'];
            $model->user_name = $_POST['StorageSystem']['user_name'];
            $model->password = $_POST['StorageSystem']['password'];
            if($model->save()) {
                $this->redirect(array('listSystem'));
            }          
        }

        $this->render('createsystem',array(
            'model'=>$model,
        ));
    }

    /**
     *   Deletes a particular model.
     *   If deletion is successful, the browser will be redirected to the 'index' page.
     */
    public function actionDeleteSystem()
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            if(isset($_GET['id']))
            {
                $model=StorageSystem::model()->findByPk($_GET['id']);
                $model->delete();
            }

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(array('listSystem'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    /**
     *    Returns the data model based on the primary key given in the GET variable.
     *    If the data model is not found, an HTTP exception will be raised.
     */
    private function loadModel()
    {
        if($this->_model===null)
        {
            if(isset($_GET['id']))
            {
               $this->_model=User::model()->findByPk($_GET['id']);
            }
            if($this->_model===null)
            {
                throw new CHttpException(404,'The requested page does not exist.');
            }
        }
        return $this->_model;
    }
}
